<template>
  <div class="wrapper">
    <div class="top">
      <div class="top__header">
        <div class="iconfont top__header__back" @click="handelBack">
          &#xe681;
        </div>
        <span>确认订单</span>
      </div>
      <div class="top__receiver">
        <div class="top__receiver__title">收货地址</div>
        <div class="top__receiver__address">
          北京理工大学国防科技园2号楼10层
        </div>
        <div class="top__receiver__info">
          <span class="info__name">瑶妹（先生）</span>
          <span class="info__number">18911024266</span>
        </div>
        <div class="iconfont top__receiver__enter">&#xe681;</div>
      </div>
    </div>

    <div class="products">
      <div class="products__tilte">{{ shopName }}</div>
      <div class="product__wrapper">
        <div class="product">
          <div
            class="product__item"
            v-for="item in productListchecked"
            :key="item._id"
          >
            <img :src="item.imgUrl" alt="" class="product__item__img" />
            <div class="product__item__detail">
              <h4 class="product__item__title">{{ item.name }}</h4>
              <p class="product__item__price">
                <span class="product__item__yen"
                  >&yen;{{ item.price }}<i>X</i>{{ item.count }}</span
                >
                <span class="product__item__total"
                  >&yen;{{ (item.price * item.count).toFixed(2) }}</span
                >
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="order">
      <div class="order__price">
        实付金额 &yen;<span>{{ price }}</span>
      </div>
      <div class="order__btn" @click="handelClickSubmit(true)">提交订单</div>
    </div>
    <div class="mask" v-if="showMask" @click.self="handelClickSubmit(false)">
      <div class="mask___content">
        <div class="mask__content__title">确认要离开收银台？</div>
        <div class="mask__content__desc">请尽快完成支付，否则将被取消</div>
        <div class="mask__content__btns">
          <div class="mask__content__btn1 " @click="handelConfrimOrder(true)">
            取消订单
          </div>
          <div class="mask__content__btn2" @click="handelConfrimOrder(false)">
            确认支付
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useRoute, useRouter } from 'vue-router'
import { useCommonCartEffect } from '../../effects/commonCartEffect'
import { post } from '../../utils/request.js'
import { useStore } from 'vuex'
import { ref } from 'vue'
const usehandelComfrimEffect = (productList, shopId, shopName) => {
  const router = useRouter()
  const store = useStore()
  const productListchecked = {}
  const products = []
  for (const key in productList.value) {
    const product = productList.value[key]
    if (productList.value[key].check && productList.value[key]?.count > 0) {
      productListchecked[key] = productList.value[key]
      products.push({
        id: product._id,
        num: product.count
      })
    }
  }
  const handelConfrimOrder = async isCanceled => {
    try {
      const result = await post('api/order', {
        addressId: 1,
        shopId,
        shopName: shopName.value,
        isCanceled,
        products
      })
      if (result?.errno === 0) {
        if (!isCanceled) {
          router.push('/orderList')
          store.commit('clearCartData', { shopId })
        } else {
          router.push('/')
        }
      }
    } catch (e) {}
  }
  return { handelConfrimOrder, productListchecked }
}
export default {
  name: 'Order',
  setup(props) {
    const route = useRoute()
    const router = useRouter()
    const shopId = parseInt(route.params.id)
    const showMask = ref(false)
    const { productList, shopName, price } = useCommonCartEffect(shopId)
    const { handelConfrimOrder, productListchecked } = usehandelComfrimEffect(
      productList,
      shopId,
      shopName
    )

    const handelBack = () => {
      router.back()
    }
    const handelClickSubmit = status => {
      showMask.value = status
    }
    return {
      productListchecked,
      shopName,
      productList,
      price,
      handelBack,
      handelConfrimOrder,
      showMask,
      handelClickSubmit
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../../style/common.scss';
.wrapper {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #eee;
  width: 100%;
}
.top {
  height: 1.96rem;
  background-image: linear-gradient(0deg, rgba(0, 145, 255, 0) 4%, #0091ff 50%);
  background-size: 100% 1.59rem;
  background-repeat: no-repeat;
  padding: 0 0.18rem;

  .top__header {
    padding-top: 0.26rem;
    line-height: 0.2rem;
    color: #fff;
    font-size: 0.16rem;
    display: flex;
    align-items: center;
    position: relative;
    .top__header__back {
      font-size: 0.22rem;
    }
    span {
      flex: 1;
      text-align: center;
    }
  }
  .top__receiver {
    position: absolute;
    top: 0.68rem;
    left: 0.18rem;
    right: 0.18rem;
    z-index: 2;
    height: 1.11rem;
    background: #fff;
    border-radius: 0.04rem;
    padding: 0.16rem 0.16rem 0.12rem 0.16rem;
    box-sizing: border-box;
    .top__receiver__title {
      font-size: 0.16rem;
      color: #333333;
      margin-bottom: 0.14rem;
      line-height: 0.22rem;
    }
    .top__receiver__address {
      font-family: PingFangSC-Regular;
      font-size: 0.14rem;
      color: #333333;
      margin-bottom: 0.06rem;
      line-height: 0.2rem;
    }
    .top__receiver__info {
      font-size: 12px;
      color: #666666;
    }
    .top__receiver__enter {
      position: absolute;
      right: 0.16rem;
      top: 50%;
      transform: translateY(-50%) rotate(180deg);
      color: #666;
    }
  }
}
.products {
  margin-bottom: 0.6rem;
  width: 100%;
  overflow: hidden;
  .products__tilte {
    padding: 0.16rem 0 0.16rem 0.16rem;
    font-size: 0.16rem;
    line-height: 0.22rem;
    color: #333;
    background: #fff;
    margin: 0 0.18rem;
  }
}
.product__wrapper {
  padding: 0 0.18rem;
}
.product {
  background: $bgColor;
  box-sizing: border-box;
  overflow-y: scroll;
  .product__item {
    display: flex;
    padding: 0.12rem 0;
    margin: 0 0.16rem;
    border-bottom: 0.01rem solid $content-bgColor;
    align-items: center;
    .product__item__check {
      font-size: 0.2rem;
      margin-right: 0.2rem;
      color: #ccc;
    }
    .product__item__check__active {
      color: #0091ff;
    }
    .product__item__img {
      width: 0.46rem;
      height: 0.46rem;
      margin-right: 0.16rem;
    }
    .product__item__title {
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $content-fontColor;
      margin-bottom: 0.06rem;
      @include ellipsis;
    }
    .product__item__detail {
      overflow: hidden;
      flex: 1;
    }
    .product__item__price {
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: #e93b3b;
      display: flex;
      align-items: center;
      i {
        margin: 0 0.05rem;
      }
      .product__item__yen {
        font-size: 0.12rem;
      }
      .product__item__total {
        flex: 1;
        text-align: right;
        color: #000000;
      }
      .product__item__origin {
        color: #999;
        text-decoration: line-through;
        margin-left: 0.06rem;
        font-size: 0.2rem;
        transform: scale(0.5);
      }
    }
  }
}
.order {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  line-height: 0.5rem;
  background: #ffffff;
  display: flex;
  .order__price {
    flex: 1;
    font-size: 0.14rem;
    color: #333333;
    margin-left: 0.24rem;
    span {
      font-size: 0.16rem;
      color: #151515;
      line-height: 0.2rem;
    }
  }
  .order__btn {
    width: 0.98rem;
    line-height: 0.5rem;
    background: #4fb0f9;
    text-align: center;
    font-size: 0.14rem;
    color: #ffffff;
  }
}
.mask {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 4;
  .mask___content {
    width: 80%;
    height: 1.57rem;
    background: #fff;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 5;
    border-radius: 0.04rem;
    text-align: center;
    .mask__content__title {
      font-size: 0.18rem;
      margin-top: 0.24rem;
      margin-bottom: 0.08rem;
      line-height: 0.25rem;
      color: #333333;
      font-weight: 700;
    }
    .mask__content__desc {
      font-size: 0.14rem;
      color: #666666;
      margin-bottom: 0.24rem;
    }
    .mask__content__btns {
      display: flex;
      justify-content: center;
      .mask__content__btn1 {
        border: 0.01rem solid #4fb0f9;
        border-radius: 0.16rem;
        width: 0.8rem;
        height: 0.32rem;
        color: #0091ff;
        text-align: center;
        line-height: 0.32rem;
        font-size: 0.14rem;
        margin-right: 0.24rem;
      }
      .mask__content__btn2 {
        width: 0.8rem;
        height: 0.32rem;
        background: #4fb0f9;
        border: 0.01rem solid #4fb0f9;
        border-radius: 0.16rem;
        text-align: center;
        line-height: 0.32rem;
        font-size: 0.14rem;
        color: #fff;
      }
    }
  }
}
</style>
